package com.erp.module.faHuoDanJu.faHuo2

import android.app.Activity
import android.content.Intent
import android.provider.MediaStore
import android.view.View
import android.widget.AdapterView
import androidx.appcompat.app.AlertDialog
import com.erp.activity.BaseActivity
import com.erp.activity.DanjianivActivity
import com.erp.myAdapter.AddFaHuoKAdapter
import com.erp.bean.FaHuoBean
import com.erp.mclass.UnCeHandler
import com.erp.R
import com.erp.util.Dialogutil
import com.erp.util.Myutil
import com.erp.view.MyAlerDialog
import kotlinx.android.synthetic.main.activity_addfahuo.*
import kotlinx.android.synthetic.main.header_knoya.*

/**
 * Created by Administrator on 2018/2/5/005.
 * 作者：刘洋
 * 单据拍照上传界面
 */

class DanJuPaiZhaoShangChuanAvtivity : BaseActivity(), AdapterView.OnItemClickListener, View.OnClickListener, AddFaHuoKView, AdapterView.OnItemLongClickListener {

    private val a = "LingShi3Avtivity"
    lateinit var listphoto: MutableList<FaHuoBean.photo>
    lateinit var deletelistphoto:MutableList<FaHuoBean.photo>
    lateinit var gvPhotoAdapter : AddFaHuoKAdapter
    lateinit var KP : AddFaHuoKP
    lateinit var dialog: AlertDialog
    var type = 0
    var CarNumBle = ""
    var ShopName = ""
    override fun getLayoutId(): Int {
        activityname = a
        return R.layout.activity_addfahuo
    }

    override fun initCreate() {
        CarNumBle = intent.getStringExtra("CarNumber")
        ShopName = intent.getStringExtra("ShopName")
        type = intent.getIntExtra("type",0)
        KP= IAddFaHuoKP(this, this, CarNumBle, ShopName, type)
        listphoto = FaHuoCarAvtivity.photo
        deletelistphoto = ArrayList()
        gvPhotoAdapter = AddFaHuoKAdapter(listphoto, this)
        GvAddFaHuo.adapter = gvPhotoAdapter
        GvAddFaHuo.onItemClickListener = this
        GvAddFaHuo.onItemLongClickListener = this
        BtShangchuan.setOnClickListener(this)
        iv_header_left.visibility=View.VISIBLE
        iv_header_left.setOnClickListener(this)
        when(type){
            FaHuoCarAvtivity.FaHuo -> {
                tv_header_title.text="发货清单"
            }
            FaHuoCarAvtivity.WuLiu -> {
                tv_header_title.text="物流单据"
            }
        }
    }

    override fun onClick(v: View?) {
        when(v){
            BtShangchuan -> {
                dialog = Myutil.showupdatedialog(this@DanJuPaiZhaoShangChuanAvtivity, "正在上传照片", false)
                KP.AddPhoto(listphoto,deletelistphoto)
            }
            iv_header_left -> {
                finish()
            }
        }
    }

    override fun onItemClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
        when(parent){
            GvAddFaHuo -> {
                if (position == listphoto.size) {
                    Dialogutil.photodialog(this@DanJuPaiZhaoShangChuanAvtivity, null)
                } else if(position<listphoto.size) {
                    val intent = Intent(this@DanJuPaiZhaoShangChuanAvtivity, DanjianivActivity::class.java)
                    intent.putExtra("url", listphoto[position].url)
                    startActivity(intent)
                }
            }
        }
    }

    override fun onItemLongClick(parent: AdapterView<*>?, view: View?, position: Int, id: Long): Boolean {
        when(parent){
            GvAddFaHuo -> {
                val myAlerDialog = MyAlerDialog(this@DanJuPaiZhaoShangChuanAvtivity)
                myAlerDialog.start()
                myAlerDialog.setdata("您确定要删除这张照片吗？")
                myAlerDialog.setleft("确定") {
                    if(listphoto[position].fid!=""){
                        deletelistphoto.add(listphoto[position])
                    }
                    listphoto.removeAt(position)
                    gvPhotoAdapter.notifyDataSetChanged()
                    myAlerDialog.dismiss()
                }
                myAlerDialog.setright("取消") {myAlerDialog.dismiss()}
                myAlerDialog.show()
                return true
            }
        }
        return false
    }


    override fun AddPhotoOK() {
        Myutil.MyTost("添加成功")
        dialog.dismiss()
        val resultIntent = Intent()
        setResult(Activity.RESULT_OK, resultIntent)
        finish()
    }

    override fun AddPhotoNo() {
        Myutil.MyTost("添加失败")
        dialog.dismiss()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (data == null) {
            return
        }
        if (resultCode == Activity.RESULT_OK) {
            when (requestCode) {
                99 -> {
                    val selectedImage = data.data
                    val filePathColumns = arrayOf(MediaStore.Images.Media.DATA)
                    if(selectedImage != null){
                        val c = contentResolver.query(selectedImage, filePathColumns, null, null, null)
                        c!!.moveToFirst()
                        val columnIndex = c.getColumnIndex(filePathColumns[0])
                        val imagePath = c.getString(columnIndex)
                        addphoto(imagePath)
                    }
                }
                100 -> {
                    val name = data.getStringExtra("dir")
                    addphoto(name)
                }
            }
        }
    }


    override fun getPhotoNo() {
        dialog.dismiss()
    }

    private fun addphoto(name: String) {
        UnCeHandler.buzhou="addphoto"
        val bean = FaHuoBean.photo()
        bean.url=name
        listphoto.add(bean)
        gvPhotoAdapter.notifyDataSetChanged()
    }
}
